const userApi = require('../../../api/user.js')
const api = require('../../../utils/request.js')
Page({
  data: {
    pageType: '',
    userId: '',
    userInfo: {},
    bottomPadding: '',
    avatarUrl: wx.getStorageSync('userInfo')
      ? wx.getStorageSync('userInfo').avatarUrl
      : '',
    nickname: wx.getStorageSync('userInfo')
      ? wx.getStorageSync('userInfo').nickName
      : ''
  },
  async getPhoneNumber(e) {
    let code = e.detail.code
    if (code) {
      let res = await userApi.getPhoneInfo({ code })
      if (res.data || res.msg.length === 11) {
        //flag:1手机号，2头像，3昵称
        await userApi.updateInfo({ flag: '1', phoneNumber: res.msg.length === 11 ? res.msg : res.data })
        wx.showToast({
          title: '手机号绑定成功',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      wx.showToast({ title: '取消绑定', icon: 'success', duration: 2000 })
    }
  },
  chooseavatar: async function (e) {
    let tempUrl = ''
    await this.upload(e.detail.avatarUrl).then((res) => {
      tempUrl = res
    })
    this.setData({
      avatarUrl: tempUrl
    })
    //flag:1手机号，2头像，3昵称
    userApi.updateInfo({ flag: '2', avatarUrl: tempUrl })
  },
  upload(file) {
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: `${api.BASE_URL}/system/oss/upload`,
        filePath: file,
        name: 'file',
        header: {
          'content-type': 'multipart/form-data',
          Authorization: wx.getStorageSync('loginInfo').Authorization
        },
        formData: {},
        success: function (res) {
          resolve(JSON.parse(res.data).data.url)
        },
        fail: function (res) { }
      })
    })
  },
  clickCancel: function () {
    wx.navigateBack()
  },
  commitNickname: function () {
    userApi
      .updateInfo({ flag: '3', nickName: this.data.nickname })
      .then((res) => {
        if (res.code === 200) {
          wx.showToast({
            title: '修改成功'
          })
        }
      })
  },
  bindInputNickname: function (e) {
    this.setData({
      nickname: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    this.setData({
      pageType: options.type
    })
    this.setData({ bottomPadding: getApp().globalData.bottomPadding })
    switch (this.data.pageType) {
      case 'avatar':
        wx.setNavigationBarTitle({
          title: '修改头像'
        })
        break
      case 'nickname':
        wx.setNavigationBarTitle({
          title: '修改昵称'
        })
        break
      case 'cellphone':
        wx.setNavigationBarTitle({
          title: '修改手机号'
        })
        break

      default:
        break
    }
    this.userId = wx.getStorageSync('loginInfo').userId
    this.setData({
      nickname: wx.getStorageSync('userInfo')
        ? wx.getStorageSync('userInfo').nickName
        : '',
      avatarUrl: wx.getStorageSync('userInfo')
        ? wx.getStorageSync('userInfo').avatarUrl
        : ''
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() { }
})
